﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using STLGROUP.Layers.Presentations.FormBase;
using STLGROUP.Layers.DataAccessLayers;
using STLGROUP.Layers.Common;
//using QuanLyBanHang.Forms.ManagerForms;
using STLGROUP.Layers.BLL;

namespace QuanLyBanHang
{
    /// <summary>
    /// MF002 - Quan Lý nhà cung cấp
    /// </summary>
    /// <author>viet sang</author>
    /// <date>15/04/2012</date>
    public partial class MF002 : frmManager
    {
        #region Variables

        DT_NhaCungCapBLL _business;

        #endregion

        #region Contructors

        public MF002()
        {
            InitializeComponent();

            InitializeForm();
        }

        #endregion

        #region Protected methods

        protected override void LoadData()
        {
            this._bindingSource.DataSource = Cache.Context.DT_NhaCungCap;//_business.GetData();
            this.dgvNhaCungCap.DataSource = _bindingSource;
            base.LoadData();
        }

        protected override void ImportFormExcel()
        {
            ImportDataFromExcel<DT_NhaCungCap>(dgvNhaCungCap);
        }

        protected override void ExportToExcel()
        {
            ExportDataToExcel(dgvNhaCungCap);
        }

        protected override void AddDataBinding()
        {
            txtMaNCC.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "MaNhaCungCap", true);
            txtTenNCC.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "TenNhaCungCap", true);
            txtNguoiDaiDien.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "NguoiDaiDien", true);
            txtChucVu.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "ChucVu", true);
            txtEmail.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "Email", true);            
            txtSoDienThoai.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "SoDienThoai", true);
            txtDiaChi.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "DiaChi", true);
            txtFax.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "Fax", true);

            cboTinhThanh.DataSource = Cache.Context.DM_TinhThanh;
            cboTinhThanh.ValueMember = "IDTinhThanh";
            cboTinhThanh.DisplayMember = "TenTinhThanh";
            cboTinhThanh.DataBindings.Add(Const.BIND_SELECTED_VALUE, _bindingSource, "IDTinhThanh", true);

            TinhThanh.DataSource = Cache.Context.DM_TinhThanh;
            TinhThanh.ValueMember = "IDTinhThanh";
            TinhThanh.DisplayMember = "TenTinhThanh";

            base.AddDataBinding();
        }

        protected override void RefreshData()
        {
            base.RefreshData();
            UICommon.ShowDataNotFound(dgvNhaCungCap, lblTimKiem, false);
        }

        protected override void AddNew()
        {
            base.AddNew();            
            txtTenNCC.Focus();
        }

        protected override int UpdateData()
        {

            if (base.UpdateData() > 0)
            {
                IQueryable<DT_NhaCungCap> lstData = this._bindingSource.DataSource as IQueryable<DT_NhaCungCap>;
                _business.UpdateNhaCungCap(lstData);
                return base.UpdateData();
            }
            LoadData();

            return -1;         
        }

        protected override void Search()
        {
            string tuKhoa = tscbxTimKiem.Text;

            var data = from e in Cache.Context.DT_NhaCungCap

                       where e.TenNhaCungCap.Contains(tuKhoa) ||
                       e.MaNhaCungCap.Contains(tuKhoa) ||
                       e.NguoiDaiDien.Contains(tuKhoa) ||
                       e.DiaChi.Contains(tuKhoa) ||
                       e.Email.Contains(tuKhoa) ||
                       e.SoDienThoai.Contains(tuKhoa) ||
                       e.Fax.Contains(tuKhoa)

                       select e;

            List<DT_NhaCungCap> lstData = data.ToList<DT_NhaCungCap>();

            //Hien thi thong bao khi khong tim thay du lieu
            UICommon.ShowDataNotFound(dgvNhaCungCap, lblTimKiem, lstData.Count == 0);

            _bindingSource.DataSource = data;
            dgvNhaCungCap.DataSource = _bindingSource;
        }

        #endregion

        #region Private methods

        private void InitializeForm()
        {
            _business = new DT_NhaCungCapBLL();
            UICommon.AddTrackingColumn(this.dgvNhaCungCap);
            this.dgvNhaCungCap.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dgvNhaCungCap_RowPostPaint);
            this.dgvNhaCungCap.DataError += new DataGridViewDataErrorEventHandler(dgvNhaCungCap_DataError);
            this.txtMaNCC.ReadOnly = true;
        }

        #endregion

        #region Events

        private void dgvNhaCungCap_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dg = (DataGridView)sender;
            // Current row record
            string rowNumber = (e.RowIndex + 1).ToString();

            // Format row based on number of records displayed by using leading zeros
            while (rowNumber.Length < dg.RowCount.ToString().Length)
            {
                rowNumber = "0" + rowNumber;
            }

            // Position text
            SizeF size = e.Graphics.MeasureString(rowNumber, this.Font);
            if (dg.RowHeadersWidth < (int)(size.Width + 20))
            {
                dg.RowHeadersWidth = (int)(size.Width + 20);
            }

            // Use default system text brush
            Brush b = SystemBrushes.ControlText;

            // Draw row number
            e.Graphics.DrawString(rowNumber, dg.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
        }

        private void dgvNhaCungCap_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void btnTinhThanh_Click(object sender, EventArgs e)
        {
            UICommon.ShowPopup(new CF002(), StructsEnums.EnumDanhMuc.TinhThanh, cboTinhThanh, TinhThanh);
        }

        #endregion

       
    }
}
